package site.yunnong.atvris.portal.spider.v191;

import com.geccocrawler.gecco.pipeline.Pipeline;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zjh
 * @date 2021/8/28 17:13
 */
public class V191VideoPipeline implements Pipeline<V191Video>, ApplicationContextAware {
    
    public static Pattern pattern = Pattern.compile("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?.mp4");
    
    @Override
    public void process(V191Video v191Video) {
        Matcher matcher = pattern.matcher(v191Video.getScript());
        if (matcher.find()) {
            v191Video.setVideoUrl(matcher.group());
        }
        v191Video.setFromUrl("191视频 - 农技社区服务平台");
        V191ImportService service = (V191ImportService) context.getBean("v191ImportServiceImpl");
        service.importSingleVideo(v191Video);
//        System.out.println(JSON.toJSONString(v191Video));
    }

    private ApplicationContext context;
    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }
}
